﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="ComboBoxItemStyleKey" TargetType="{x:Type ComboBoxItem}">
        <Setter Property="HorizontalContentAlignment"
                Value="{Binding Path=HorizontalContentAlignment,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
        <Setter Property="VerticalContentAlignment"
                Value="{Binding Path=VerticalContentAlignment,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
        <Setter Property="Padding" Value="3,1,3,1" />
        <Setter Property="Background" Value="{DynamicResource ComboBoxItemBackgroundKey}" />
        <Setter Property="Foreground" Value="{DynamicResource ComboBoxItemForegroundKey}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                    <Border Name="Bd" Margin="2,1,2,1" Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"
                            SnapsToDevicePixels="true" CornerRadius="4">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsHighlighted" Value="true">
                            <Setter TargetName="Bd" Property="Background"
                                    Value="{DynamicResource HighlightedComboBoxItemBackgroundKey}" />
                            <Setter Property="Foreground" Value="{DynamicResource HighlightedComboBoxItemForegroundKey}" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground"
                                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ComboBoxFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <!--<Rectangle Margin="4,4,21,4"
                               StrokeThickness="1"
                               Stroke="Black"
                               StrokeDashArray="1 2"
                               SnapsToDevicePixels="true"/>-->
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ComboBoxEditableTextBox" TargetType="{x:Type TextBox}">
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="Foreground" Value="{DynamicResource TextBoxForegroundKey}" />
        <Setter Property="Background" Value="{DynamicResource TextBoxBackgroundKey}" />
        <Setter Property="BorderBrush" Value="{DynamicResource TextBoxBorderKey}" />
        <Setter Property="AllowDrop" Value="true" />
        <Setter Property="MinWidth" Value="0" />
        <Setter Property="MinHeight" Value="0" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"
                            SnapsToDevicePixels="True">

                        <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden"
                                VerticalScrollBarVisibility="Hidden" Background="Transparent" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Geometry x:Key="DownArrowGeometry">M 0 0 L 3.5 4 L 7 0 Z</Geometry>

    <Style x:Key="ComboBoxReadonlyToggleButton" TargetType="{x:Type ToggleButton}">
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="IsTabStop" Value="false" />
        <Setter Property="Focusable" Value="false" />
        <Setter Property="ClickMode" Value="Press" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="4">
                        <Grid Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"
                                HorizontalAlignment="Right">
                            <Rectangle Name="ArrowRectangle" RadiusX="2" RadiusY="2" Margin="2" Width="13"
                                    HorizontalAlignment="Right" Fill="{DynamicResource ArrowRectangleNormalFill}" />
                            <Path Name="Arrow" Margin="0,1,0,0" Fill="White" Data="{StaticResource DownArrowGeometry}"
                                    HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="Arrow" Property="Fill" Value="#AFAFAF" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="ArrowRectangle" Property="Fill"
                                    Value="{DynamicResource ArrowRectangleWarmFill}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="IsTabStop" Value="false" />
        <Setter Property="Focusable" Value="false" />
        <Setter Property="ClickMode" Value="Press" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="4">
                        <Grid Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"
                                HorizontalAlignment="Right">
                            <Rectangle Name="ArrowRectangle" RadiusX="2" RadiusY="2" Margin="2" Width="13"
                                    HorizontalAlignment="Right" Fill="{DynamicResource ArrowRectangleNormalFill}" />
                            <Path Name="Arrow" Margin="0,1,0,0" Fill="White" Data="{StaticResource DownArrowGeometry}"
                                    HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="Arrow" Property="Fill" Value="#AFAFAF" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="ArrowRectangle" Property="Fill"
                                    Value="{DynamicResource ArrowRectangleWarmFill}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">
        <Grid Name="Placement" SnapsToDevicePixels="true">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Popup Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" Placement="Bottom"
                    IsOpen="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"
                    PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"
                    MinWidth="{Binding ElementName=Placement,Path=ActualWidth}"
                    MaxHeight="{TemplateBinding MaxDropDownHeight}">

                <Border x:Name="DropDownBorder" BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"
                        CornerRadius="4">
                    <ScrollViewer CanContentScroll="true">
                        <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </ScrollViewer>
                </Border>
            </Popup>

            <Border x:Name="Border" Grid.ColumnSpan="2" BorderThickness="{TemplateBinding BorderThickness}"
                    Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
                    CornerRadius="4" SnapsToDevicePixels="True" />
            <TextBox Name="PART_EditableTextBox" Margin="{TemplateBinding Padding}" BorderThickness="0"
                    IsReadOnly="{Binding Path=IsReadOnly,RelativeSource={RelativeSource TemplatedParent}}"
                    HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                    VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
            <ToggleButton Grid.Column="1" Style="{StaticResource ComboBoxToggleButton}"
                    IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" />

        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsKeyboardFocusWithin" Value="true">
                <Setter Property="Foreground" Value="Black" />
            </Trigger>
            <Trigger Property="HasItems" Value="false">
                <Setter TargetName="DropDownBorder" Property="Height" Value="95" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                <Setter Property="Background" Value="{DynamicResource ComboBoxDisabledBackgroundKey}" />
                <Setter Property="BorderBrush" Value="{DynamicResource ComboBoxDisabledBorderBrushKey}" />
            </Trigger>
            <Trigger Property="IsGrouping" Value="true">
                <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
            </Trigger>
            <Trigger SourceName="PART_Popup" Property="Popup.HasDropShadow" Value="true">
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="{x:Type ComboBox}" TargetType="{x:Type ComboBox}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ComboBoxFocusVisual}" />
        <Setter Property="Foreground" Value="{DynamicResource ComboBoxForegroundKey}" />
        <Setter Property="Background" Value="{DynamicResource ComboBoxBackgroundKey}" />
        <Setter Property="BorderBrush" Value="{DynamicResource ComboBoxBorderBrushKey}" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="Padding" Value="4,3" />
        <Setter Property="ItemContainerStyle" Value="{StaticResource ComboBoxItemStyleKey}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Grid Name="MainGrid" SnapsToDevicePixels="true">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition
                                    MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"
                                    Width="0" />
                        </Grid.ColumnDefinitions>
                        <Popup Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" Placement="Bottom"
                                IsOpen="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"
                                PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"
                                MinWidth="{Binding ElementName=MainGrid,Path=ActualWidth}"
                                MaxHeight="{TemplateBinding MaxDropDownHeight}">

                            <Border x:Name="DropDownBorder" BorderThickness="{TemplateBinding BorderThickness}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    Background="{TemplateBinding Background}" CornerRadius="4">
                                <ScrollViewer CanContentScroll="true">
                                    <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained"
                                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                </ScrollViewer>
                            </Border>
                        </Popup>
                        <ToggleButton Grid.ColumnSpan="2" Background="{TemplateBinding Background}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Style="{StaticResource ComboBoxReadonlyToggleButton}"
                                IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" />
                        <ContentPresenter IsHitTestVisible="false" Margin="{TemplateBinding Padding}"
                                Content="{TemplateBinding SelectionBoxItem}"
                                ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger SourceName="PART_Popup" Property="Popup.HasDropShadow" Value="true">
                        </Trigger>
                        <Trigger Property="HasItems" Value="false">
                            <Setter TargetName="DropDownBorder" Property="Height" Value="95" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground"
                                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                            <Setter Property="Background" Value="{DynamicResource ComboBoxDisabledBackgroundKey}" />
                            <Setter Property="BorderBrush" Value="{DynamicResource ComboBoxDisabledBorderBrushKey}" />
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsEditable" Value="true">
                <Setter Property="IsTabStop" Value="false" />
                <Setter Property="Padding" Value="3" />
                <Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}" />
            </Trigger>
        </Style.Triggers>
    </Style>
    
</ResourceDictionary>